

								
varying		vec4		v_fragmentColor;	
varying		vec2		v_texCoord;
	
// for texture
uniform		sampler2D	u_texture;	
	
// share CXFORM
uniform		int			u_use_cxform;			
uniform		vec4		u_cxform_mult_color;	
uniform		vec4		u_cxform_add_color;		

// for gradient	
uniform		vec4		u_ColorControl[8];	
uniform		float		u_RatioControl[8];
uniform		float		u_isLinear;
				
										
// 0 = Normal - 1 = Textured - 2 = Gradient
uniform		int			u_ActiveShader; 
		
		
		
												
void fragment_normal(){										
	if( u_use_cxform != 0){								
		gl_FragColor.r = max(0, min(((v_fragmentColor.r * u_cxform_mult_color.r) ) + u_cxform_add_color.r, 1));
		gl_FragColor.g = max(0, min(((v_fragmentColor.g * u_cxform_mult_color.g) ) + u_cxform_add_color.g, 1));
		gl_FragColor.b = max(0, min(((v_fragmentColor.b * u_cxform_mult_color.b) ) + u_cxform_add_color.b, 1));
		gl_FragColor.a = max(0, min(((v_fragmentColor.a * u_cxform_mult_color.a) ) + u_cxform_add_color.a, 1));
	}	
	else {				
		gl_FragColor = v_fragmentColor;
	}																																		
}																												




void fragment_textured(){
	gl_FragColor =  texture2D(u_texture, v_texCoord);

	if( u_use_cxform != 0){																					
		gl_FragColor.r = max(0, min(((gl_FragColor.r * u_cxform_mult_color.r) ) + u_cxform_add_color.r, 1));
		gl_FragColor.g = max(0, min(((gl_FragColor.g * u_cxform_mult_color.g) ) + u_cxform_add_color.g, 1));
		gl_FragColor.b = max(0, min(((gl_FragColor.b * u_cxform_mult_color.b) ) + u_cxform_add_color.b, 1));
		gl_FragColor.a = max(0, min(((gl_FragColor.a * u_cxform_mult_color.a) ) + u_cxform_add_color.a, 1));
	}	
}



void fragment_gradient(){
	float current_ratio = 0.0;																						
	if(u_isLinear > 0.1){																						
		current_ratio = (v_texCoord.x + 16384) / 32768;															
	}																											
	else {																										
		current_ratio = clamp( ( sqrt(v_texCoord.x * v_texCoord.x + v_texCoord.y * v_texCoord.y ) ) / 16384, 0.0, 1.0 ); 
	}																												
																																																						
	if( current_ratio <= u_RatioControl[0] ){																		
		gl_FragColor =  u_ColorControl[0];																			
	}																												
	else if(current_ratio <= u_RatioControl[1]){																	
		float coefL = u_RatioControl[0];																			
		vec4 colorL = u_ColorControl[0];																			
		float coefR = u_RatioControl[1]; 																			
		vec4 colorR = u_ColorControl[1]; 																			
																													
		if( coefR > 1.0 ){																							
			gl_FragColor = colorL;																					
		}																											
		else{																										
			float coefDistance = coefR - coefL;																		
			float coefBetween = (current_ratio - coefL) / coefDistance;												
																													
			gl_FragColor =  (colorL*(1.0-coefBetween)) + (colorR*(coefBetween));									
		}																											
	}																												
	else if(current_ratio <= u_RatioControl[2]){																	
		float coefL = u_RatioControl[1];																			
		vec4 colorL = u_ColorControl[1];																			
		float coefR = u_RatioControl[2]; 																			
		vec4 colorR = u_ColorControl[2]; 																			
																													
		if( coefR > 1.0 ){																							
			gl_FragColor = colorL;																					
		}																											
		else{																										
			float coefDistance = coefR - coefL;																		
			float coefBetween = (current_ratio - coefL) / coefDistance;												
																													
			gl_FragColor =  (colorL*(1.0-coefBetween)) + (colorR*(coefBetween));									
		}																											
	}																												
	else if(current_ratio <= u_RatioControl[3]){																	
		float coefL = u_RatioControl[2];																			
		vec4 colorL = u_ColorControl[2];																			
		float coefR = u_RatioControl[3]; 																			
		vec4 colorR = u_ColorControl[3]; 																			
																													
		if( coefR > 1.0 ){																							
			gl_FragColor = colorL;																					
		}																											
		else{																										
			float coefDistance = coefR - coefL;																		
			float coefBetween = (current_ratio - coefL) / coefDistance;												
																													
			gl_FragColor =  (colorL*(1.0-coefBetween)) + (colorR*(coefBetween));									
		}																											
	}																												
	else if(current_ratio <= u_RatioControl[4]){																	
		float coefL = u_RatioControl[3];																			
		vec4 colorL = u_ColorControl[3];																			
		float coefR = u_RatioControl[4]; 																			
		vec4 colorR = u_ColorControl[4]; 																			
																													
		if( coefR > 1.0 ){																							
			gl_FragColor = colorL;																					
		}																											
		else{																										
			float coefDistance = coefR - coefL;																		
			float coefBetween = (current_ratio - coefL) / coefDistance;												
																													
			gl_FragColor =  (colorL*(1.0-coefBetween)) + (colorR*(coefBetween));									
		}																											
	}																												
	else if(current_ratio <= u_RatioControl[5]){																	
		float coefL = u_RatioControl[4];																			
		vec4 colorL = u_ColorControl[4];																			
		float coefR = u_RatioControl[5]; 																			
		vec4 colorR = u_ColorControl[5]; 																			
																													
		if( coefR > 1.0 ){																							
			gl_FragColor = colorL;																					
		}																											
		else{																										
			float coefDistance = coefR - coefL;																		
			float coefBetween = (current_ratio - coefL) / coefDistance;												
																													
			gl_FragColor =  (colorL*(1.0-coefBetween)) + (colorR*(coefBetween));									
		}																											
	}																												
	else if(current_ratio <= u_RatioControl[6]){																	
		float coefL = u_RatioControl[5];																			
		vec4 colorL = u_ColorControl[5];																			
		float coefR = u_RatioControl[6]; 																			
		vec4 colorR = u_ColorControl[6]; 																			
																													
		if( coefR > 1.0 ){																							
			gl_FragColor = colorL;																					
		}																											
		else{																										
			float coefDistance = coefR - coefL;																		
			float coefBetween = (current_ratio - coefL) / coefDistance;												
																													
			gl_FragColor =  (colorL*(1.0-coefBetween)) + (colorR*(coefBetween));									
		}																											
	}																												
	else if(current_ratio <= u_RatioControl[7]){																	
		float coefL = u_RatioControl[6];																			
		vec4 colorL = u_ColorControl[6];																			
		float coefR = u_RatioControl[7]; 																			
		vec4 colorR = u_ColorControl[7]; 																			
																													
		if( coefR > 1.0 ){																							
			gl_FragColor = colorL;																					
		}																											
		else{																										
			float coefDistance = coefR - coefL;																		
			float coefBetween = (current_ratio - coefL) / coefDistance;												
																													
			gl_FragColor =  (colorL*(1.0-coefBetween)) + (colorR*(coefBetween));									
		}																											
	}																												
	else{																											
		gl_FragColor = u_ColorControl[7];																			
	}																												
																																																									
	if( u_use_cxform != 0){																							
		gl_FragColor.r = max(0, min(((gl_FragColor.r * u_cxform_mult_color.r) ) + u_cxform_add_color.r, 1));		
		gl_FragColor.g = max(0, min(((gl_FragColor.g * u_cxform_mult_color.g) ) + u_cxform_add_color.g, 1));		
		gl_FragColor.b = max(0, min(((gl_FragColor.b * u_cxform_mult_color.b) ) + u_cxform_add_color.b, 1));		
		gl_FragColor.a = max(0, min(((gl_FragColor.a * u_cxform_mult_color.a) ) + u_cxform_add_color.a, 1));		
	}																												
}



void main(){
	if(u_ActiveShader == 0){
		fragment_normal();
	}
	else if(u_ActiveShader == 1){
		fragment_textured();
	}
	else if(u_ActiveShader == 2){
		fragment_gradient();
	}
}
